#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
//全局变量的定义
int score;
int high,width;
int enemy_x1,enemy_y1;
int enemy_x2,enemy_y2;
int enemy_x3,enemy_y3;
int enemy_x4,enemy_y4;
int position_x,position_y;
void gotoxy(int x, int y) //将光标调整到(x,y)的位置
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
}

void HideCursor()  //隐藏光标显示
{
    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()//数据的初始化
{
	high=25;
	width=45;
	score=0;
	enemy_x1=high/3;
	enemy_y1=width/4;
	enemy_x2=high;
	enemy_y2=width/8;
	enemy_x3=high/6;
	enemy_y3=width/3;
	enemy_x4=high/7;
	enemy_y4=width/3;
	position_x=high/2;
	position_y=width/2;
	
	HideCursor();
	
}

struct tm //定义时间结构体，包括时分秒和10毫秒 
{ 
	int minutes,seconds; 
	int hscd; 
}time,tmp,total; //time用以计时显示，tmp用以存储上一阶段时间，total记总时间 
int cnt; 

FILE* fout; 
//每次调用update函数，相当于时间过了10ms 
void update(struct tm *t) 
{ 
	
	(*t).hscd++; //10ms单位时间加1 
	cnt++; 
	if ((*t).hscd==100) //计时满1s，进位 
	{ 
		(*t).hscd=0; 
		(*t).seconds++; 
	} 
	if ((*t).seconds==60) //计时满一分，进位 
	{ 
		(*t).seconds=0; 
		(*t).minutes++; 
	} 
	
	Sleep(10); //Sleep是windows提供的函数，作用是暂停程序，单位毫秒，所以此处暂停10ms 
} 
void display(struct tm *t) 
{ 
	//此处输出计时结果，\r为回车不换行，既一直在同一行更新时间 
	
	printf("%d:",(*t).minutes); 
	printf("%d:",(*t).seconds); 
	printf("%d\r",(*t).hscd); 
	//printf("Now, press ‘e’ key to stop the clock…"); 
} 
void time_init() //初始化时间 
{ 
	time.minutes=time.seconds=time.hscd=0; 
} 
void get_total() //计算总时间 
{ 
	total.hscd = cnt % 100; 
	cnt /= 100; 
	total.seconds = cnt % 60; 
	cnt /= 60; 
	total.minutes = cnt % 60; 
	
} 
void show()//显示画面
{
    
    gotoxy(0, 0); //使光标回到0，0
	
	int i,j;
	for(i=0;i<=high;i++)
	{	
		for(j=0;j<=width;j++)
		{
			if((i==enemy_x1)&&(j==enemy_y1))
				printf("#");
			
			else if((i==enemy_x3)&&(j==enemy_y3))
				printf("#");
			
			else if((i==enemy_x2)&&(j==enemy_y2))
				printf("#");
			
			else if((i==enemy_x4)&&(j==enemy_y4))
				printf("#");
			else if((i==position_x)&&(j==position_y))
				printf("@");
			
			
			else
				printf(" ");
		}
		printf("\n");
		
	}	
	

}

void updateWithoutIput()//与用户输入无关的更新
{
	
	if(enemy_x3>position_x)
	
		if (enemy_x1>position_x)
			enemy_x1--;
		else if (enemy_x1<position_x)
			enemy_x1++;
		if (enemy_y1>position_y)
			enemy_y1--;
		else if (enemy_y1<position_y)
			enemy_y1++;
		
		if(enemy_y2>0)
		{enemy_y2--;}
		else
		{
			enemy_y2 = width;
			int randPosition = rand() % (high-5);
			enemy_x2 = randPosition ;
			enemy_y2 = randPosition + high/2;
		}
		enemy_y2--;
		
		
		if (enemy_x3>position_x)
			enemy_x3--;
		else if (enemy_x3<position_x)
			enemy_x3++;
		if (enemy_y3>position_y)
			enemy_y3--;
		else if (enemy_y3<position_y)
			enemy_y3++;
		
		if(enemy_x4>0)
		{enemy_x4--;}
		else
		{
			enemy_x4 = high;
			int randPosition = rand() % (width-2);
			enemy_y4 = randPosition ;
			enemy_x4 = randPosition + width/2;
		}
		enemy_x4--;
		
		Sleep(100);
		
}

void updateWithInput()//与用户输入有关的更新
{
	
	//用户输入
	char input;
	if (_kbhit())
	{
		input = getch();
		
		if(input=='a')
			position_y--;
		if(input=='d')
			position_y++;
		if(input=='s')
			position_x++;
		if(input=='w')
			position_x--;
		
	}
	
}

int main()
{
    startup();//初始化
    char m; 
	time_init(); 
	cnt = 0; 
	fout = fopen("timeout.txt","w"); 
	
	while(1) 
	{ 
        show();//显示画面  
        updateWithoutIput();//与用户输入无关的更新  //更新数据
        updateWithInput(); //与用户输入有关的更新  //输入分析
		if(kbhit()) //此处检查是否有键盘输入 
		{ 
			m=getch(); 
			if(m == '\r') 
				break; 
			else if(m ==  ' ') 
			{ 
				tmp = time; //记录上一段计时器结果 
				fprintf(fout,"%d:%d:%d\n",tmp.minutes,tmp.seconds,tmp.hscd); //写入文件 
				time_init(); 
				printf("\n"); 
			} 
			else 
			{ 
				printf("输入错误，仅支持输入回车键或者空格键!\n"); 
			} 
		} 
		update(&time); //更新计时器 
		display(&time); //显示计时器时间 
	} 
	tmp = time; //输出最后一次即使结果，写入文件 
	fprintf(fout,"%d:%d:%d\n",tmp.minutes,tmp.seconds,tmp.hscd); 
	get_total(); //计算总的时间，显示，并写入文件 
	printf("\n总时间:%d:%d:%d\n",total.minutes,total.seconds,total.hscd); 
	fprintf(fout,"统计时间:%d:%d:%d\n",total.minutes,total.seconds,total.hscd); 
	fclose(fout); 
	
	getch(); 
	
    system("pause");
    return 0;
}
